package com.sc.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.sc.dto.TrainingDirectionsInsertDTO;
import com.sc.dto.TrainingDirectionsPageDTO;
import com.sc.dto.TrainingDirectionsUpdateDTO;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.sc.entity.TrainingDirections;
import com.sc.service.TrainingDirectionsService;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.List;

/**
 * 培训方向表 控制层。
 *
 * @author JoeZhou
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/api/v1/trainingDirections")
public class TrainingDirectionsController {

    @Autowired
    private TrainingDirectionsService trainingDirectionsService;

    /**
     * 添加培训方向表。
     *
     * @param trainingDirectionsInsertDTO 培训方向表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
/*    @PostMapping("save")
    public boolean save(@RequestBody TrainingDirections trainingDirections) {
        return trainingDirectionsService.save(trainingDirections);
    }*/
    @PostMapping("save")
    public boolean save(@RequestBody TrainingDirectionsInsertDTO trainingDirectionsInsertDTO) {
        TrainingDirections trainingDirections = BeanUtil.copyProperties(trainingDirectionsInsertDTO,TrainingDirections.class);
        return trainingDirectionsService.save(trainingDirections);
    }

    /**
     * 根据主键删除培训方向表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return trainingDirectionsService.removeById(id);
    }

    /**
     * 根据主键更新培训方向表。
     *
     * @param trainingDirectionsUpdateDTO 培训方向表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody TrainingDirectionsUpdateDTO trainingDirectionsUpdateDTO) {
        TrainingDirections trainingDirections = BeanUtil.copyProperties(trainingDirectionsUpdateDTO, TrainingDirections.class);
        return trainingDirectionsService.updateById(trainingDirections,true);
    }

    /**
     * 查询所有培训方向表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<TrainingDirections> list() {
        return trainingDirectionsService.list();
    }

    /**
     * 根据培训方向表主键获取详细信息。
     *
     * @param id 培训方向表主键
     * @return 培训方向表详情
     */
    @GetMapping("getInfo/{id}")
    public TrainingDirections getInfo(@PathVariable Serializable id) {
        return trainingDirectionsService.getById(id);
    }

    /**
     * 分页查询培训方向表。
     *
     * @param trainingDirectionsPageDTO 分页对象
     * @return 分页对象
     */
    @PostMapping("page")
    public Page<TrainingDirections> page(@RequestBody TrainingDirectionsPageDTO trainingDirectionsPageDTO) {
        Page<TrainingDirections> page= Page.of(trainingDirectionsPageDTO.getPageNum(),trainingDirectionsPageDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("name",trainingDirectionsPageDTO.getName());
        return trainingDirectionsService.pageAs(page,queryWrapper,TrainingDirections.class);
    }

}
